package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.PaymentSecurityLogs;
import com.example.mall.dto.PaymentSecurityLogsDto;
import com.example.mall.mapper.PaymentSecurityLogsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.PaymentSecurityLogsQuery;
import com.example.mall.service.IPaymentSecurityLogsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 支付安全记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class PaymentSecurityLogsServiceImpl extends ServiceImpl<PaymentSecurityLogsMapper, PaymentSecurityLogs> implements IPaymentSecurityLogsService {

    @Autowired
    private PaymentSecurityLogsMapper paymentSecurityLogsMapper;

    @Override
    public PageDTO<PaymentSecurityLogsDto> queryPage(PaymentSecurityLogsQuery paymentSecurityLogsQuery) {
        if (paymentSecurityLogsQuery == null) {
            paymentSecurityLogsQuery = new PaymentSecurityLogsQuery();
        }
        Page<PaymentSecurityLogs> page = paymentSecurityLogsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<PaymentSecurityLogs> cartPage = lambdaQuery()
                .like(paymentSecurityLogsQuery.getLogId()!= null, PaymentSecurityLogs::getLogId, paymentSecurityLogsQuery.getLogId())
                .like(paymentSecurityLogsQuery.getPaymentId()!= null, PaymentSecurityLogs::getPaymentId, paymentSecurityLogsQuery.getPaymentId())
                .like(paymentSecurityLogsQuery.getRiskLevel()!= null, PaymentSecurityLogs::getRiskLevel, paymentSecurityLogsQuery.getRiskLevel())
                .like(paymentSecurityLogsQuery.getRiskReason()!= null, PaymentSecurityLogs::getRiskReason, paymentSecurityLogsQuery.getRiskReason())
                .like(paymentSecurityLogsQuery.getUserId()!= null, PaymentSecurityLogs::getUserId, paymentSecurityLogsQuery.getUserId())
                .orderByDesc(PaymentSecurityLogs::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, PaymentSecurityLogsDto.class);
    }

    @Override
    public PaymentSecurityLogs queryPaymentSecurityLogsById(Long logId) {
        AssertUtil.isNotNull(logId, "logId不能为空");
        return paymentSecurityLogsMapper.selectById(logId);
    }

    @Override
    public List<PaymentSecurityLogs> queryPaymentSecurityLogsist() {
        return this.list();
    }

    @Override
    public void insert(PaymentSecurityLogsDto paymentSecurityLogsDto) {
        PaymentSecurityLogs paymentSecurityLogs = new PaymentSecurityLogs();
        CopyUtils.copyProperties(paymentSecurityLogsDto, paymentSecurityLogs);
        paymentSecurityLogs.setLogId(CodeGenerateUtils.generateUniqueId());
        paymentSecurityLogs.setCreateDate(new Date());
        paymentSecurityLogs.setUpdateDate(new Date());
        paymentSecurityLogsMapper.insert(paymentSecurityLogs);
    }

    @Override
    public void update(PaymentSecurityLogsDto paymentSecurityLogsDto) {
        AssertUtil.isNotNull(paymentSecurityLogsDto.getLogId(), "logId不能为空");
        PaymentSecurityLogs paymentSecurityLogs = paymentSecurityLogsMapper.selectById(paymentSecurityLogsDto.getLogId());
        AssertUtil.isNotNull(paymentSecurityLogs, "支付安全记录表不存在");
        CopyUtils.copyProperties(paymentSecurityLogsDto, paymentSecurityLogs);
        paymentSecurityLogs.setUpdateDate(new Date());
        paymentSecurityLogsMapper.updateById(paymentSecurityLogs);
    }

    @Override
    public void delete(Long logId) {
        AssertUtil.isNotNull(logId, "logId不能为空");
        PaymentSecurityLogs paymentSecurityLogs = paymentSecurityLogsMapper.selectById(logId);
        AssertUtil.isNotNull(paymentSecurityLogs, "支付安全记录表不存在");
        paymentSecurityLogsMapper.deleteById(logId);
    }

    @Override
    public void deleteBatch(List<Long> logIds) {
        AssertUtil.isNotNull(logIds, "logIds不能为空");
        List<PaymentSecurityLogs> paymentSecurityLogs = paymentSecurityLogsMapper.selectBatchIds(logIds);
        AssertUtil.isNotNull(paymentSecurityLogs, "支付安全记录表不存在");
        paymentSecurityLogsMapper.deleteBatchIds(logIds);
    }
}
